#include "StdAfx.h"
#include "PlayGenerator.h"
using namespace SHGame;
PlayGenerator::PlayGenerator(void)
{

}

Card^ playPicker(Trick^ t, LinkedList<Card^> ^ hand, Player^ p, PlayInfo ^pi, bool trump, bool follow, bool alone) {
	if (pi->HasDefWTrump()) {
		// we have to be careful
		if (!trump)
			return pi->GetSmallTrump(hand);
		else
			return pi->GetBigTrump(hand);
	} else {
		// we can just take it.
		return pi->PerfectCard(hand, t->get(t->getOwner()));
	}
}

Card^ playPartener(Trick^ t, LinkedList<Card^> ^ hand, Player^ p, PlayInfo ^pi, bool trump, bool follow) {
	if (follow) {
		if (pi->HasPicker()) {
			// picker behind us
			if (trump)
				return pi->GetSmallTrump(hand);
			else
				return pi->GetSchmear(hand);
		} else {
			// the picker has already played
			if (trump)
				if (pi->KnownPickTeam(t->getOwner()))
					return pi->GetSmallTrump(hand);
				else
					return pi->GetBigTrump(hand);
			else
				if (pi->KnownPickTeam(t->getOwner()))
					return pi->GetSchmear(hand);
				else
					return pi->GetThrowOff(hand);
		}
	} else {
		if (pi->HasPicker()) {
			// picker behind us
			return hand->First->Value;
		} else {
			// the picker has already played
			return hand->First->Value;
		}
	}
}

Card^ playDefender(Trick^ t, LinkedList<Card^> ^ hand, Player^ p, PlayInfo ^pi, bool trump, bool follow) {
	Player^ own = t->getOwner();
	Card^ top = t->get(own);
	if (pi->HasPickTeamWTrump()) {
		if (pi->CouldBePickTeam(own)) {
			if ( pi->CanTake(hand, top)) {
				return pi->GetBigTrump(hand);
			} else {
				return pi->GetThrowOff(hand);
			}
		} else {
			return pi->GetThrowOff(hand);
		}
	} else {
		if (pi->CanTake(hand, top)) {
				return pi->PerfectCard(hand, top);
			} else {
				return pi->GetThrowOff(hand);
			}
	}

}

Card^ playLeaster(Trick^ t, LinkedList<Card^> ^ hand, Player^ p, PlayInfo ^pi, bool trump, bool follow) {
	if (pi[p]->Points < 5) {
		if (t->getPoints() < 20) {
			if (pi->CanTake(hand, t->get(t->getOwner()))) {
				return pi->GetBigTrump(hand);
			} else {
				return pi->GetSchmear(hand);
			}
		} else {
			return pi->GetThrowOff(hand);
		}
	} else {
		if (t->getPoints() < 20) {
			return pi->GetSchmear(hand);
		} else {
			return pi->GetThrowOff(hand);
		}
	}
}

Card^ PlayGenerator::getPlay(Trick^ t, CardSet^ hand, Player^ p, PlayInfo ^pi) {
	Card^ lead = t->getLead();
	Player^ own = t->getOwner();
	Card^ owncard = t->get(own);
	LinkedList<Card^> ^handl = hand->followSuit(lead);
	if (handl->Count == 0) {
		Diagnostics::Debug::WriteLine("ERROR! THERE ARE NO VALID PLAYS?!", "PLAYGEN");
	} else if (handl->Count == 1) {
		// no choice to make here... just play the first!
		Diagnostics::Debug::WriteLine("Playing the only card I can : " + handl->First->Value->ToString(), "PLAYGEN");
		
		return handl->First->Value;
	}
	bool trump = lead->isTrump();
	bool follow;
	if (trump)
		follow = ( lead->isTrump() && handl->First->Value->isTrump() );
	else
		follow = ( lead->onSuit(handl->First->Value) );

	// delegate to the other methods:
	if (pi->pick == nullptr) {
		// leaster
		return playLeaster(t, handl, p, pi, trump, follow);
	} else if ( pi->pick->Equals(p) ) {
		// play for picker
		return playPicker(t, handl, p, pi, trump, follow, pi->partener == nullptr);
	} else if ( pi->partener->Equals(p) ) {
		return playPartener(t, handl, p, pi, trump, follow);
	} else {
		return playDefender(t, handl, p, pi, trump, follow);
	}
}
Card^ PlayGenerator::getLead(CardSet^ hand, Player^ p, PlayInfo ^pi) {
	if (pi->AmIPickTeam(p)) {
		return hand->GetATrump();
	} else {
		// play non pickteam
		if (pi->PickTeamOnEnd())
			return hand->GetShortFail();
		else
			return hand->GetLongFail();
	}
}

array<Card^>^ PlayGenerator::getBury(CardSet^ hand)
{
	array<Card^>^ arr = { hand->get(6), hand->get(5) };
	return arr;
}
bool PlayGenerator::wantPick(CardSet^ hand)
{
	return hand->GetStrength() > PICK_THRESHOLD;
}